﻿// Decompiled with JetBrains decompiler
// Type: MCM.Implementation.FluentBuilder.DefaultSettingsPresetBuilder
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using MCM.Abstractions;
using MCM.Abstractions.Base;
using MCM.Abstractions.FluentBuilder;
using System;
using System.Collections.Generic;
using System.Linq;

#nullable enable
namespace MCM.Implementation.FluentBuilder
{
  internal sealed class DefaultSettingsPresetBuilder : ISettingsPresetBuilder
  {
    private string Id { get; }

    private string Name { get; }

    private IDictionary<string, object?> PropertyValues { get; } = (IDictionary<string, object>) new Dictionary<string, object>();

    public DefaultSettingsPresetBuilder(string id, string name)
    {
      this.Id = id;
      this.Name = name;
    }

    public ISettingsPresetBuilder SetPropertyValue(string property, object? value)
    {
      if (!this.PropertyValues.ContainsKey(property))
        this.PropertyValues[property] = value;
      return (ISettingsPresetBuilder) this;
    }

    public ISettingsPreset Build(BaseSettings settings)
    {
      return (ISettingsPreset) new MemorySettingsPreset(settings.Id, this.Id, this.Name, (Func<BaseSettings>) (() =>
      {
        List<ISettingsPropertyDefinition> list = settings.GetAllSettingPropertyDefinitions().ToList<ISettingsPropertyDefinition>();
        foreach (KeyValuePair<string, object> propertyValue in (IEnumerable<KeyValuePair<string, object>>) this.PropertyValues)
        {
          string overridePropertyId = propertyValue.Key;
          object obj = propertyValue.Value;
          ISettingsPropertyDefinition propertyDefinition = list.Find((Predicate<ISettingsPropertyDefinition>) (x => x.Id == overridePropertyId));
          if (propertyDefinition != null)
            propertyDefinition.PropertyReference.Value = obj;
        }
        return settings;
      }));
    }
  }
}
